package it.stasbranger.myproject.service;

import com.google.gwt.requestfactory.shared.Locator;
import com.google.gwt.requestfactory.shared.ServiceLocator;

import it.stasbranger.myproject.domain.SuppliersHasRequests;

import org.apache.commons.lang.StringUtils;

import org.springframework.beans.BeansException;

import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

import org.springframework.stereotype.Component;

/**
 * Implements GWT Request Factory Locator for both Entities and Service, acts as an adapter between the GWT RequestFactory
 * contract and the JPA/Spring back end.
 * This class is designed to enable integration of Entities that may have composite primary keys and that may not have a version field etc.
 * It is also designed to push requests from GWT through a standard DAO and Service layer
 */
@Component
public class SuppliersHasRequestsGWTAdapter extends Locator<SuppliersHasRequests, String>
		implements ServiceLocator, ApplicationContextAware {
	public final static String KEY_DELIMITER = "|||";

	@Override
	public SuppliersHasRequests create(Class<? extends SuppliersHasRequests> clazz) {
		return new SuppliersHasRequests();
	}

	@Override
	public SuppliersHasRequests find(Class<? extends SuppliersHasRequests> clazz, String id) {
		Object[] keyValues = getPrimaryKeysFromGoogleKey(id);
		Integer SuppliersIdsuppliers = (Integer) keyValues[0];
		Integer RequestsIdrequests = (Integer) keyValues[1];

		return getSuppliersHasRequestsService().findSuppliersHasRequestsByPrimaryKey(SuppliersIdsuppliers, RequestsIdrequests);
	}

	@Override
	public Class<SuppliersHasRequests> getDomainType() {
		return SuppliersHasRequests.class;
	}

	@Override
	public String getId(SuppliersHasRequests domainObject) {
		return createGoogleKey(domainObject);
	}

	@Override
	public Class<String> getIdType() {
		return String.class;
	}

	@Override
	public Object getVersion(SuppliersHasRequests suppliersHasRequests) {
		// If your entity has a version field, return it here, the scaffolding engine did not detect a version field for this entity
		// we have generated code to return a version that is based on the contents of the fields of the Entity, GWT uses the version number to detect changes to Entity Proxies
		final int prime = 31;
		int result = 1;
		result = prime * result + ((suppliersHasRequests.getSuppliersIdsuppliers() == null) ? 0 : suppliersHasRequests.getSuppliersIdsuppliers().hashCode());
		result = prime * result + ((suppliersHasRequests.getRequestsIdrequests() == null) ? 0 : suppliersHasRequests.getRequestsIdrequests().hashCode());
		result = prime * result + ((suppliersHasRequests.getSenddate() == null) ? 0 : suppliersHasRequests.getSenddate().hashCode());
		result = prime * result + ((suppliersHasRequests.getPrice() == null) ? 0 : suppliersHasRequests.getPrice().hashCode());
		return result;
	}

	public Object getInstance(Class<?> clazz) {
		if (it.stasbranger.myproject.service.SuppliersHasRequestsService.class.equals(clazz))
			return getSuppliersHasRequestsService();

		return null;
	}

	/**
	 * Returns the Primary Key field values in the order they are required by the DAO layer to find by primary key
	 * @param key
	 * @return
	 */
	protected Object[] getPrimaryKeysFromGoogleKey(String key) {
		String[] keyValuesAsText = StringUtils.split(key, KEY_DELIMITER);

		Object[] keyValues = new Object[keyValuesAsText.length];
		keyValues[0] = new Integer(keyValuesAsText[0]);
		keyValues[1] = new Integer(keyValuesAsText[1]);

		return keyValues;
	}

	/**
	 * Returns a single String that can uniquely identify an instance to the GWT Client
	 * The primary key values are encoded in the order they are required by the DAO layer to findByPrimaryKey for convenience
	 * @param object
	 * @return
	 */
	protected String createGoogleKey(SuppliersHasRequests suppliersHasRequests) {
		StringBuilder key = new StringBuilder();
		key.append(suppliersHasRequests.getSuppliersIdsuppliers());
		key.append(KEY_DELIMITER);
		key.append(suppliersHasRequests.getRequestsIdrequests());
		return key.toString();
	}

	/**
	 * 
	 */
	public SuppliersHasRequestsService getSuppliersHasRequestsService() {
		SuppliersHasRequestsService service;

		service = (SuppliersHasRequestsService) context.getBean("SuppliersHasRequestsService");

		return service;
	}

	// Spring Context Management
	private static ApplicationContext context;

	/**
	 * The Spring Context will be pushed in when the container is initialized
	 */
	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
		context = applicationContext;
	}
}
